$(document).ready(function(){
	getAdvertisement();
});

function getAdvertisement(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/advertisement/get-advertisement',
            type:'post',
            data:function(d){
            },
            error:getError
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "banner_config", "bSortable": false },
            { "data": "sort", "bSortable": true },
            { "data": "banner_config", "bSortable": false },
            { "data": "platform", "bSortable":false },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<a href="javascript:" onclick="advertisementDetail('+ full.id +')">修改</a>';
                }
            },
            {
                "targets": [1],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return data[0].picture_name;
                }
            },
            {
                "targets": [2],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<input type="text" '+(data == 1 ? 'checked="checked"' : '')+' value="'+data+'" class="form-control"><a href="javascript:" onclick="changeAdvertisementSort('+ full.id +',this)">&nbsp;&nbsp;修改</a>';
                }
            },
            {
                "targets": [3],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<a href="javascript:;" onclick="showImg(\''+data[0].picture_url+'\')">查看图片</a>';
                }
            },
            {
                "targets": [4],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return data == 1 ? 'pc' : 'wap';
                }
            },
            {
                "targets": [5],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
                }
            }
        ],
        order: [[ 2, "asc"]]
    });
}

function table_list_reload(){
	var table = $('#table_list').DataTable();
    table.ajax.reload();
}

function advertisementDetail(id){
	$.post('/advertisement/advertisement-detail',{id:id},success,'json');
	return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200){
        	$('#advertisement-modal').modal('show');
        	var result = data.val;
        	var banner = result.banner_config;
        	$('#platform').val(result.platform);
        	$('#picture_name').val(banner.picture_name);
        	$('#sort').val(result.sort);
        	if(banner.picture_url){
        		$('#picture_url').val(banner.picture_url);
        		$('#picture_url').prev().removeClass('hide');
        	}
        	$('#product_url').val(banner.product_url);
        	$('#use_flg').val(result.use_flg);
        	$('#hot_sale_id').val(result.id);
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
}

function changeAdvertisementSort(id,where){
	var sort = $(where).prev().val();
	$.post('/advertisement/change-advertisement-sort',{id:id,sort:sort},success,'json');
    return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200){
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { table_list_reload(); }, 1000);
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
}

function changeAdvertisementStatus(id,use_flg){
	title = '确定停用此活动？';
	if(use_flg == 1){
		title = '确定启用此活动？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/advertisement/change-advertisement-status',{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200){
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
            if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
        }
    });
}

$('#advertisement_form').delegate('.upload-img-btn','click',function(){
    $('.upload_file_ot').trigger('click');
});

$('.upload_file_ot').change(function(){
    if(this.value != ''){
    	$('#upload_form_update').submit();
    }
});

$('#upload_form_update').submit(function(e){
    e.preventDefault();
    $(this).ajaxSubmit({
        success:function(data){
            var result = $.parseJSON(data);
            if(result.code == 500){
                $.jBox.error(result.val, '错误');
            }else{
            	var imageBathUrl = $('#imageBathUrl').val();
            	$('#picture_url').val(result.val);
            	$('#picture_url').prev().removeClass('hide');
            	$('input[type=file]').closest('form').get(0).reset();
            }
        }
    });
});

function showImg(img_url){
	if(img_url == '' || img_url == null || img_url == undefined){
        $.jBox.error('数据为空', '错误');
        return false;
    }
	var json = {
        "title": "分类图片", //相册标题
        "id": 0, //相册id
        "start": 0, //初始显示的图片序号，默认0
        "data": [   //相册包含的图片，数组格式
            {
                "alt": "",//图片名
                "pid": 0, //图片id
                "src": $('#imageBathUrl').val() + img_url, //原图地址
                "thumb": "" //缩略图地址
            }
        ]
    };
    layer.photos({
        photos: json
    });
}

$('.ajax_post_tag').on('click',function(){
	var form = $('#advertisement_form');
    var picture_name = $('#picture_name').val();
    if(picture_name == ''){
        $.jBox.error('请填写图片名称', '错误');
        return false;
    }
    var sort = $('#sort').val();
	if(sort == ''){
        $.jBox.error('请填写活动排序编号', '错误');
        return false;
    }
	var picture_url = $('#picture_url').val();
	if(picture_url == ''){
        $.jBox.error('请上传一张商品图片', '错误');
        return false;
    }
	var product_url = $('#product_url').val();
	if(product_url == ''){
        $.jBox.error('请填写商品链接', '错误');
        return false;
    }
    $.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#advertisement-modal').modal('hide');
                table_list_reload();
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
	}
});

$('.add-advertisement').on('click',function(){
	$('#advertisement-modal').modal('show');
	$('#picture_name').val('');
	$('#platform').val('1');
	$('#sort').val('');
	$('#picture_url').val('');
	$('#picture_url').prev().addClass('hide');
	$('#product_url').val('');
	$('#use_flg').val(1);
	$('#hot_sale_id').val('');
});

$('table').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	changeAdvertisementStatus(id,use_flg);
});